package com.wwg.security;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * **供应链科技有限公司 版权所有 © Copyright 2019
 *
 * @since Created in 2019-08-31
 */
public class DesensitizationUtils {


    private final static String DESENSITIZATION = "****";
    private final static String DESENSITIZATION_THREE = "***";
    private final static String DESENSITIZATION_TEL_PHONE = "-****";
    private final static String DESENSITIZATION_CHAR = "*";

    /**
     * 电话号码脱敏
     *
     * @param str 电话号码
     * @return
     */
    public static String desensitizationPhone(String str) {
        return desensitization(str, 3, 4);
    }

    /**
     * 名字脱敏
     *
     * @param name 名字
     * @return
     */
    public static String desensitizationName(String name) {
        if(ObjectUtils.isEmpty(name)){
            return null;
        }
        String newName = null;
        if (name.length()>0 && name.length()<=2){
            newName = DesensitizationUtils.desensitization(name,1,0);
        }
        if(name.length()>2){
            newName = DesensitizationUtils.desensitization(name,2,0);
        }
        return newName;
    }

    /**
     * 身份证号码脱敏
     *
     * @param idNo 身份证号码
     * @return
     */
    public static String desensitizationIdNo(String idNo) {
        return desensitization(idNo, 5, 4);
    }


    /**
     * 字符串自定义脱敏
     * @param str 脱敏的字符串
     * @param start 前start位不脱敏
     * @param end 后end位不脱敏
     * @return
     */
    public static String desensitization(String str, int start, int end) {
        if (null == str || str.length() <= start || start < 0) {
            return str;
        }
        int len = str.length();
        String encryptionStr = str.substring(0, start) + DESENSITIZATION;
        if (len <= (start + end)) {
            return encryptionStr;
        }
        encryptionStr += str.substring(len - end);
        return encryptionStr;
    }

    private final static String DASH  = "-";

    public static String desensitizationTelPhone(String target) {
        return desensitizationTelPhone(target,3,4);
    }
    /**
     * 座机号脱敏为固定电话：“-”符号后的4位使用*展示，其余显示明文。
     * 没有“-”的按前三位后四位脱敏 如***123****
     * 如021-****1234,0558-****123
     * @return
     */
    public static String desensitizationTelPhone(String target,int start,int end) {
        // 非null
        if (target == null) {
            return null;
        }
        int length = target.length();
        if (length < 4) {
            return target;
        }
        // 如果已经有*，则直接返回
        if (target.contains("*")){
            return target;
        }

        if (!target.contains(DASH)) {
            //座机不规范的没有-的位数大于等于11位按前三后四脱敏，小于11位的按前四位脱敏
            if (length>=11) {
                String showStr = target.substring(start, length -end);
                return DESENSITIZATION_THREE + showStr + DESENSITIZATION;
            } else {
                return target.replaceFirst("[0-9]{4}",DESENSITIZATION);
            }
        }
        return target.replaceFirst("-[0-9]{" +
                end +
                "}", DESENSITIZATION_TEL_PHONE);
    }

//    /**
//     * 座机号脱敏
//     * @return
//     */
//    public static String desensitizationTelPhone(String target) {
//        // 非null
//        if (null == target || target.length() <= 7) {
//            return target;
//        }
//        if (!target.contains(DASH)) {
//            String showStr = target.substring(3,target.length()-4);
//            return DESENSITIZATION_THREE + showStr + DESENSITIZATION;
//        }
//        return target.replaceFirst("-[0-9]{4}", DESENSITIZATION_TEL_PHONE);
//    }

    public static void main(String[] args) {
        System.out.println(desensitizationTelPhone("185****3016"));
        System.out.println(desensitizationTelPhone("**1116"));
        System.out.println(desensitizationTelPhone("185****3012316"));
        System.out.println(desensitizationTelPhone("185**16"));
        System.out.println(desensitizationTelPhone("185*"));

        System.out.println(desensitizationTelPhone("021123132313"));
        System.out.println(desensitizationTelPhone("0211"));
        System.out.println(desensitizationTelPhone("01"));
        System.out.println(desensitizationTelPhone("021111"));
        System.out.println(desensitizationTelPhone("02111111"));
        System.out.println(desensitizationTelPhone("02111112111"));
        System.out.println(desensitizationTelPhone("02112313231311"));
        System.out.println(desensitizationTelPhone("021123132313111"));
        System.out.println(desensitizationTelPhone("021-23132313"));
    }

    /**
     * 判断是否已脱敏
     *
     * @param str 字符串
     * @return
     */
    public static boolean isDesensitization(String str) {
        return null != str && str.contains(DESENSITIZATION_CHAR);
    }

    public static String desensitizationNameNew(String str) {
        str = str.trim();
        if (StringUtils.isEmpty(str))
            return "";
        if (isAlpha(str)) {
            //a-z
            int index = str.trim().indexOf(" ");
            if (index == -1)
                return str.substring(0, 1) + linkAsterisk(str.length() - 1);
            else {
                String span = " ";
                String[] s = str.split(span);
                String s1 = s[0];
                for (int i = 1; i < s.length; ++i) {
                    s1 += span + linkAsterisk(s[i].length());
                }
                return s1;
            }
        } else
            return str.substring(0, 1) + linkAsterisk(str.length() - 1);
    }

    /**
     * 判断是否为纯英文字母
     *
     * @param str
     * @return
     */
    private static boolean isAlpha(String str) {
        str = str.replaceAll(" ", "");
        if (str == null) return false;
        return str.matches("[a-zA-Z]+");
    }

    /**
     * '*'符号连接
     *
     * @param count
     * @return
     */
    private static String linkAsterisk(int count) {
        String asterisk = "*";
        String result = "";
        for (int i = 0; i < count; ++i) {
            result += asterisk;
        }
        return result;
    }

}
